<template>
  <div>
    <vxe-modal v-model="isVisiable" width="75vw" show-footer :mask="false" destroy-on-close>
      <template #title>
        <span class="text-bold">选择流程方案</span>
      </template>
      <template #default>
        <Splitpanes style="height:65vh;">
          <Pane size="30">
            <div style="height:100%">
              <vxe-table ref="wkflwPurposeRef" show-overflow auto-resize resizable height="auto" :data="purposeList" :row-config="{ isCurrent: true }" @cell-click="purposeClick">
                <vxe-column type="seq" width="30"></vxe-column>
                <vxe-column field="purposeNo" title="编号" width="60"></vxe-column>
                <vxe-column field="purposeName" title="检验目的" min-width="120"></vxe-column>
              </vxe-table>
            </div>
          </Pane>
          <Pane size="40">
            <div style="height:100%">
              <vxe-table ref="wkflwRef" show-overflow auto-resize resizable height="auto" :data="wkflwList.filter(data=>(!curPurposeObj||!curPurposeObj.flowNo)||((curPurposeObj.flowNo&&data.workFlowNo===curPurposeObj.flowNo))||data.isActive)" :row-config="{ isCurrent: true }" @cell-click="wkflwClick">
                <vxe-column type="radio" width="30" align="center">
                  <template #radio="{ row, checked }">
                    <span class="custom-radio" :class="checked ? 'text-green' : ''" @click="setSelectRow(row)">
                      <i v-if="checked" class="vxe-icon-success-circle-fill"></i>
                      <i v-else class="vxe-icon-success-circle"></i>
                    </span>
                  </template>
                </vxe-column>
                <vxe-column type="seq" width="30"></vxe-column>
                <vxe-column field="workFlowNo" title="编号" width="60"></vxe-column>
                <vxe-column field="workFlowNa" title="流程方案" min-width="120"></vxe-column>
                <vxe-column field="isActive" title="在用" width="50" :align="'center'">
                  <template #default="{ row }">
                    <span v-if="row.isActive" class="text-orange text-bold checkbox-checked-fill">是</span>
                  </template>
                </vxe-column>
                <!-- <vxe-column field="germNo" title="微生物标本编号" width="100"></vxe-column>
                <vxe-column field="rptNo" title="报告编号" width="100"></vxe-column> -->
              </vxe-table>
            </div>
          </Pane>
          <Pane size="30">
            <div style="height:100%">
              <vxe-table ref="wkflwNodeRef" show-overflow auto-resize resizable height="auto" :data="wkflwNodeList" :row-config="{ isCurrent: true }">
                <vxe-column type="seq" width="30"></vxe-column>
                <vxe-column field="nodeNo" title="编号" width="60"></vxe-column>
                <vxe-column field="nodeDictName" title="流程节点" min-width="100"></vxe-column>
              </vxe-table>
            </div>
          </Pane>
        </Splitpanes>
      </template>
      <template #footer>
        <div class="flex justify-between">
          <div>
          </div>
          <div>
            <vxe-button @click="cancelEvent">取消</vxe-button>
            <vxe-button status="primary" @click="confirmEvent">确定</vxe-button>
          </div>
        </div>
      </template>
    </vxe-modal>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, computed, getCurrentInstance, defineProps } from "vue";
import { VxeMessage, VxeNotifyType } from '@/utils/CommonMessage';
import { Splitpanes, Pane } from 'splitpanes';
import { formatDateTime } from "@/utils/common";

let { proxy }: any = getCurrentInstance();
let isVisiable = ref(false);
let wkflwList: any[] = reactive([]);/**流程方案集合 */
let wkflwNodeList: any[] = reactive([]);/**流程方案节点集合 */
const props = defineProps({});
const wkflwRef = ref(null);/**流程方案列表对象 */
const wkflwNodeRef = ref(null);/**流程方案节点列表对象 */
const wkflwPurposeRef = ref(null);/* 检验目的列表对象 */
let currentMainInfoObj = reactive({}) as any;
let radioGroup = ref('1');
let purposeList = reactive([]);/**检验目的集合 */
let curPurposeObj = reactive({}) as any;/* 当前检验目的对象 */
defineExpose({
  showModal,/**打开选择方案的弹窗 */
});
/**流程方案单元格点击事件 */
function wkflwClick({ row, rowIndex }) {
  wkflwNodeList.length = 0;
  if (row && row.bacTestWorkFlowNodes && row.bacTestWorkFlowNodes.length > 0) {
    Object.assign(wkflwNodeList, row.bacTestWorkFlowNodes);
  }
  setTimeout(async () => {
    await wkflwRef.value?.setRadioRow(row);
    await wkflwRef.value?.setCurrentRow(row);
  }, 100);
}
/**流程方案行单选按钮选中事件 */
function setSelectRow(row) {
  wkflwRef.value?.setRadioRow(row);
}
/**页面初始化/切换显示隐藏 */
function showModal(currentMainInfo: any) {
  isVisiable.value = true;
  Object.assign(currentMainInfoObj, currentMainInfo);
  getAllPurposeList();
  getFlowList();
}
/**确认选择 */
function confirmEvent() {
  const row = wkflwRef.value?.getCurrentRecord();
  const url = '/api/bac/bac-test-work-flow/changeBacTestWorkFlowV2';
  const arrs = wkflwList.filter(p => p.isActive);
  let curUseWorkFlowNo = arrs && arrs.length > 0 ? arrs[0].workFlowNo : '';
  let selWorkFlowNo = row.workFlowNo;
  const data = { sampleNo: currentMainInfoObj.sampleNo, rptGroup: currentMainInfoObj.rptGroup, sampleda: formatDateTime(currentMainInfoObj.sampleda, 'yyyy-MM-dd 00:00:00'), reqNo: currentMainInfoObj.reqNo, curUseWorkFlowNo: curUseWorkFlowNo, selWorkFlowNo: selWorkFlowNo, purposeNo: curPurposeObj.purposeNo };
  proxy.$http
    //注意此处的数据条数
    .post(url, data)
    .then((res: any) => {
      if (res && res.status && res.status.msg) {
        VxeMessage(res.status.msg, VxeNotifyType.success);
        isVisiable.value = false;
        proxy.$emit('change');
      } else {
        VxeMessage("切换失败", VxeNotifyType.error);
      }
    })
    .catch((err: any) => {
      VxeMessage('获取培养基数据异常', VxeNotifyType.error);
    });
}
/**关闭窗体 */
function cancelEvent() {
  isVisiable.value = false;
}
/**加载所有检验目的 */
function getAllPurposeList() {
  purposeList.length = 0;
  proxy.$http
    .get('/api/bac/bacReqPurpose/findPurposeList', { params: {} })
    .then((res: any) => {
      Object.assign(purposeList, res.data);
      purposeClick({ row: purposeList[0], rowIndex: 0 });
    })
    .catch((err: any) => {
      VxeMessage('获取检验目的数据异常', VxeNotifyType)
    })
}
/**加载流程方案 */
function getFlowList() {
  wkflwList.length = 0;
  const url = '/api/bac/bac-test-work-flow/findAllWorkFlowWithCurrentUse';//'/api/bac/bac-test-work-flow/findBacTestWorkFlowListByReqNo';
  proxy.$http
    //注意此处的数据条数
    .get(url, { params: { reqNo: currentMainInfoObj.reqNo, rptGroup: currentMainInfoObj.rptGroup, sampleNo: currentMainInfoObj.sampleNo, sampleda: formatDateTime(currentMainInfoObj.sampleda, 'yyyy-MM-dd hh:mm:ss') } })
    .then(async (res: any) => {
      wkflwList.length = 0;
      Object.assign(wkflwList, res.data);
      if (res && res.data && res.data.length > 0) {
        wkflwClick({ row: res.data[0], rowIndex: 0 });
        proxy.$nextTick(async () => {
          setTimeout(async () => {
            const showList = wkflwList.filter(data => (!curPurposeObj || !curPurposeObj.flowNo) || ((curPurposeObj.flowNo && data.workFlowNo === curPurposeObj.flowNo)) || data.isActive);
            if (showList && showList.length > 0) {
              await wkflwRef.value?.setRadioRow(showList[0]);
              await wkflwRef.value?.setCurrentRow(showList[0]);

              wkflwClick({ row: showList[0], rowIndex: 0 });
            }
          }, 200);
        });
      }
    })
    .catch((err: any) => {
      VxeMessage('获取方案数据异常', VxeNotifyType.error);
    });
}
/**加载全部流程方案节点 */
function getFlowNodeList({ label }) {
  if (label == '0') {
    //getDefaultFlowNodeList({ label: '1' });
  } else {
    getFlowList();
  }
}
/**加载默认备选流程方案节点 */
function getDefaultFlowNodeList() {
  const url = '/api/bac/bac-test-work-flow-node/findDefaultBacTestWorkFlowNodeList';
  proxy.$http
    //注意此处的数据条数
    .get(url)
    .then(async (res: any) => {
      wkflwNodeList.length = 0;
      Object.assign(wkflwNodeList, res.data);
    })
}
/**检验目的行选中切换  */
function purposeClick({ row }) {
  Object.assign(curPurposeObj, row);
  proxy.$nextTick(async () => {
    await wkflwPurposeRef.value?.setCurrentRow(row);

    setTimeout(async () => {
      const showList = wkflwList.filter(data => (!curPurposeObj || !curPurposeObj.flowNo) || ((curPurposeObj.flowNo && data.workFlowNo === curPurposeObj.flowNo)) || data.isActive);
      if (showList && showList.length > 0) {
        await wkflwRef.value?.setRadioRow(showList[0]);
        await wkflwRef.value?.setCurrentRow(showList[0]);

        wkflwClick({ row: showList[0], rowIndex: 0 });
      }
    }, 200);

  });
}
</script>

<style scoped lang="scss">
.custom-radio {
  font-size: 18px;
  cursor: pointer;
  user-select: none;
}
</style>
